package com.kezesong.leasing.service.impl;

import com.alibaba.fastjson.JSON;
import com.kezesong.leasing.entity.ShopInfoEntity;
import com.kezesong.leasing.mapper.ShopInfoMapper;
import com.kezesong.leasing.service.ShopInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShopInfoServiceImpl implements ShopInfoService {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ShopInfoMapper shopInfoMapper;

    @Override
    public  List<ShopInfoEntity> getShopInfoList() {
        List<ShopInfoEntity> shopInfoEntities = shopInfoMapper.selectAll();
        if (logger.isDebugEnabled()) {
            logger.debug("================================ShopInfoServiceImpl.getShopInfoList shopInfoEntities:{}", JSON.toJSONString(shopInfoEntities));
        }
        return shopInfoEntities;
    }

    @Override
    public int addShopInfo(ShopInfoEntity shopInfoEntity) {
        return shopInfoMapper.insert(shopInfoEntity);
    }

    @Override
    public List<ShopInfoEntity> getShopInfoListByName(String shopName) {
        List<ShopInfoEntity> list = shopInfoMapper.selectAll();
        List<ShopInfoEntity> result = list.stream().filter(s -> s.getShopName().contains(shopName)).collect(Collectors.toList());
         if (logger.isDebugEnabled()) {
            logger.debug("======================ShopInfoServiceImpl.getShopInfoListByName  shopName:{}, shopInfoEntities:{}", shopName, JSON.toJSONString(result));
        }
        return result;
    }

    @Override
    public List<ShopInfoEntity> getShopInfoListByShopFlag(String shopFlag) {
        List<ShopInfoEntity> list = shopInfoMapper.selectAll();
        List<ShopInfoEntity> result = list.stream().filter(s -> s.getShopFlag().equals(shopFlag)).collect(Collectors.toList());
        if (logger.isDebugEnabled()) {
            logger.debug("======================ShopInfoServiceImpl.getShopInfoListByFlag  shopFlag:{}, shopInfoEntities:{}", shopFlag, JSON.toJSONString(result));
        }
        return result;
    }

    @Override
    public ShopInfoEntity getShopInfoById(int shopId) {
        ShopInfoEntity shopInfoEntity = new ShopInfoEntity();
        shopInfoEntity.setShopId(shopId);
        List<ShopInfoEntity> result = shopInfoMapper.select(shopInfoEntity);
        if (null != result && result.size()>0){
            return result.get(0);
        }
        return new ShopInfoEntity();
    }
}
